package com.fruit.web.vo.shopcart;

import java.util.ArrayList;

public class Inventory implements InventoryInterface,java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224733L;
	private ArrayList<InventoryItem> inventory;
    // total value of inventory
    private Double inventoryPriceTotal = 0.0;
    private Integer inventoryQuantityTotal = 0;
    private Double inventoryIntegrationTotal = 0.0;
    private boolean haveSelected;
    
    public Inventory() {
        inventory = new ArrayList<InventoryItem>();
    }
	public void addInventoryItem(InventoryItem newItem) {
		boolean found = false;
        // if iventory item exist increment quantity count. 
        InventoryItem item;
        for(int i= 0, max= inventory.size(); i < max; i++){
            item = (InventoryItem)inventory.get(i);
            if ((null == newItem.getSpecialField1() || "".equals(newItem.getSpecialField1())) && (null == newItem.getSpecialField2() || "".equals(newItem.getSpecialField2()))) {
	            if (item.getCode().equals(newItem.getCode())) {
	                item.incrementQuantity(newItem.getSalesQuantity());
	                found = true;
	                break;
	            }
            }
            if ((null != newItem.getSpecialField1() && !("".equals(newItem.getSpecialField1()))) && (null != newItem.getSpecialField2() && !("".equals(newItem.getSpecialField2())))) {
	            if ((null == item.getSpecialField1() || "".equals(item.getSpecialField1())) || (null == item.getSpecialField2() || "".equals(item.getSpecialField2()))) {
	            	continue;
	            }
            	if (item.getCode().equals(newItem.getCode()) && (item.getSpecialField1().equals(newItem.getSpecialField1())) && (item.getSpecialField2().equals(newItem.getSpecialField2()))) {
	                item.incrementQuantity(newItem.getSalesQuantity());
	                found = true;
	                break;
	            }
            }
            
        }

        // add item if not found in inventory.
        if (!found) {
            inventory.add(newItem);
            if (newItem.getSalesQuantity() == 0) {
            	newItem.incrementQuantity(1);
            }
        }

        // update invenory
        recalculateIventory();

	}
	public boolean addOnlyOneInventoryItem(InventoryItem newItem) {
		boolean found = false;
        // if iventory item exist increment quantity count. 
        InventoryItem item;
        for(int i= 0, max= inventory.size(); i < max; i++){
            item = (InventoryItem)inventory.get(i);
            if ((null == newItem.getSpecialField1() || "".equals(newItem.getSpecialField1())) && (null == newItem.getSpecialField2() || "".equals(newItem.getSpecialField2()))) {
	            if (item.getCode().equals(newItem.getCode())) {
	                item.incrementQuantity(newItem.getSalesQuantity());
	                found = true;
	                break;
	            }
            }
            if ((null != newItem.getSpecialField1() && !("".equals(newItem.getSpecialField1()))) && (null != newItem.getSpecialField2() && !("".equals(newItem.getSpecialField2())))) {
	            if ((null == item.getSpecialField1() || "".equals(item.getSpecialField1())) || (null == item.getSpecialField2() || "".equals(item.getSpecialField2()))) {
	            	continue;
	            }
            	if (item.getCode().equals(newItem.getCode()) && (item.getSpecialField1().equals(newItem.getSpecialField1())) && (item.getSpecialField2().equals(newItem.getSpecialField2()))) {
	                item.incrementQuantity(newItem.getSalesQuantity());
	                found = true;
	                break;
	            }
            }
            
        }

        // add item if not found in inventory.
        if (!found) {
            inventory.add(newItem);
            if (newItem.getSalesQuantity() == 0) {
            	newItem.incrementQuantity(1);
            }
        }

        // update invenory
        recalculateIventory();
        return found;

	}

	public ArrayList<InventoryItem> getInventory() {
		// TODO Auto-generated method stub
		return inventory;
	}
	public void setInventory(ArrayList<InventoryItem> inventoryList){
		this.inventory=inventoryList;
	}

	public double getInventoryPriceTotal() {
		// TODO Auto-generated method stub
		return inventoryPriceTotal;
	}
	
	public void setInventoryPriceTotal(Double inventoryPriceTotal) {
		this.inventoryPriceTotal = inventoryPriceTotal;
	}
	public int getInventoryQuantityTotal() {
        return inventoryQuantityTotal;
    }
	
	public void setInventoryQuantityTotal(Integer inventoryQuantityTotal) {
		this.inventoryQuantityTotal = inventoryQuantityTotal;
	}
	
	public Double getInventoryIntegrationTotal() {
		return inventoryIntegrationTotal;
	}
	public void setInventoryIntegrationTotal(Double inventoryIntegrationTotal) {
		this.inventoryIntegrationTotal = inventoryIntegrationTotal;
	}
	public void setHaveSelected(boolean haveSelected) {
		this.haveSelected = haveSelected;
	}
	public boolean isHaveSelected() {
		boolean result = false;
		if (null != this.getInventory()) {
			for (InventoryItem tempInventoryItem:inventory) {
				if (tempInventoryItem.isSelected()) {
					result = true;
				}
				if (result) {
					break;
				}
			}
		}
		return result;
	}
	public void removeInventoryItem(InventoryItem newItem,boolean removeItemOnZero) {
		// TODO Auto-generated method stub
		InventoryItem foundItem = null;
        InventoryItem item;
        for(int i= 0, max= inventory.size(); i < max; i++){
            item = (InventoryItem)inventory.get(i);
            if ((null == newItem.getSpecialField1() || "".equals(newItem.getSpecialField1())) && (null == newItem.getSpecialField2() || "".equals(newItem.getSpecialField2()))) {
		            
	            if (item.getCode().equals(newItem.getCode())) {
	                item.decrementQuantity();
	                foundItem = item;
	                break;
	            }
            }
            if ((null != newItem.getSpecialField1() && !("".equals(newItem.getSpecialField1()))) && (null != newItem.getSpecialField2() && !("".equals(newItem.getSpecialField2())))) {
	            if ((null == item.getSpecialField1() || "".equals(item.getSpecialField1())) || (null == item.getSpecialField2() || "".equals(item.getSpecialField2()))) {
	            	continue;
	            }
	            if (item.getCode().equals(newItem.getCode()) && (item.getSpecialField1().equals(newItem.getSpecialField1())) && (item.getSpecialField2().equals(newItem.getSpecialField2()))) {
	            	item.decrementQuantity();
	                foundItem = item;
	                break;
	            }
            }
        }

        // remove the item if qauntity is less then zero and is allowd by
        // removeItemOnZero
        if (removeItemOnZero && foundItem != null && foundItem.getSalesQuantity() <= 0) {
            inventory.remove(foundItem);
        }

        // update invenory
        recalculateIventory();

	}
	public void recalculateIventory() {
        Double tempInventoryPriceTotal = 0.0;
        int tempInventoryQuantityTotal = 0;
        Double tempInventoryIntegrationTotal = 0.0;
        InventoryItem item = new InventoryItem();
        if (null != this.getInventory()) {
	        for(int i= 0, max= inventory.size(); i < max; i++){
	            item = (InventoryItem)inventory.get(i);
	            tempInventoryPriceTotal += item.getSalesPrice() * item.getSalesQuantity();
	            tempInventoryQuantityTotal += item.getSalesQuantity();
	            tempInventoryIntegrationTotal +=item.getIntegration();
	        }
        }
        this.setInventoryPriceTotal(tempInventoryPriceTotal);
        this.setInventoryQuantityTotal(tempInventoryQuantityTotal);
        this.setInventoryIntegrationTotal(tempInventoryIntegrationTotal);
    }
}
